/*
 * Copyright 2009 Jesse McLaughlin (nzjess@gmail.com)
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *         http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.travelfusion.xmlclient.ri.handler.misc;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.xml.stream.XMLStreamConstants;

import org.travelfusion.xmlclient.handler.HandlesRequestsFor;
import org.travelfusion.xmlclient.impl.handler.AbstractXmlToolRequestStAXResponseHandler;
import org.travelfusion.xmlclient.ri.xobject.misc.XListSuppliersRequest;
import org.travelfusion.xmlclient.ri.xobject.misc.XListSuppliersResponse;
import org.travelfusion.xmlclient.ri.xobject.misc.XSupplier;
import org.travelfusion.xmlclient.ri.xobject.misc.XSupplier.Mode;

/**
 * @author Jesse McLaughlin (nzjess@gmail.com)
 */
@HandlesRequestsFor(XListSuppliersRequest.class)
public class ListSuppliersHandler extends AbstractXmlToolRequestStAXResponseHandler<XListSuppliersRequest, XListSuppliersResponse> {

  @Override
  protected void fillTemplate() throws Exception {
  // nop
  }

  private static enum State {
    ROOT, MODE_LIST, SUPPLIER_LIST
  }

  public XListSuppliersResponse handleResponse() throws Exception {
    XListSuppliersResponse response = new XListSuppliersResponse();
    response.setSuppliers(new ArrayList<XSupplier>());

    Map<String, XSupplier> supplierMap = new HashMap<String, XSupplier>();

    State state = State.ROOT;

    String currentName = null;
    String currentMode = null;
    XSupplier supplier = null;
    boolean build = false;

    StringBuilder builder = new StringBuilder(32);

    for (int event; ((event = responseReader.next()) != XMLStreamConstants.END_DOCUMENT);) {
      switch (event) {
        case XMLStreamConstants.START_ELEMENT:
          currentName = responseReader.getName().getLocalPart();
          builder.setLength(0);
          switch (state) {
            case ROOT:
              if ("Mode".equals(currentName)) {
                state = State.MODE_LIST;
              }
              break;
            case MODE_LIST:
              if ("SupplierList".equals(currentName)) {
                state = State.SUPPLIER_LIST;
              }
              else if ("Name".equals(currentName)) {
                build = true;
              }
              break;
            case SUPPLIER_LIST:
              if (supplier == null) {
                if ("Supplier".equals(currentName)) {
                  supplier = new XSupplier();
                  if (currentMode.equals("plane")) {
                    supplier.setMode(Mode.PLANE);
                  }
                  else if (currentMode.equals("train")) {
                    supplier.setMode(Mode.TRAIN);
                  }
                  else if (currentMode.equals("hotel")) {
                    supplier.setMode(Mode.HOTEL);
                  }
                  else if (currentMode.equals("car")) {
                    supplier.setMode(Mode.CAR);
                  }
                }
              }
              if ("Name".equals(currentName)) {
                build = true;
              }
              if ("Enabled".equals(currentName)) {
                build = true;
              }
              if ("Visible".equals(currentName)) {
                build = true;
              }
              break;
          }
          break;
        case XMLStreamConstants.CHARACTERS:
          if (build) {
            builder.append(responseReader.getTextCharacters(), responseReader.getTextStart(), responseReader.getTextLength());
          }
          break;
        case XMLStreamConstants.END_ELEMENT:
          currentName = responseReader.getName().getLocalPart();
          switch (state) {
            case MODE_LIST:
              if ("Name".equals(currentName)) {
                currentMode = builder.toString();
                build = false;
              }
              if ("Mode".equals(currentName)) {
                state = State.ROOT;
              }
              break;
            case SUPPLIER_LIST:
              if (supplier != null) {
                if ("Name".equals(currentName)) {
                  supplier.setName(builder.toString());
                  build = false;
                }
                if ("Enabled".equals(currentName)) {
                  supplier.setEnabled(Boolean.parseBoolean(builder.toString()));
                  build = false;
                }
                if ("Visible".equals(currentName)) {
                  supplier.setVisible(Boolean.parseBoolean(builder.toString()));
                  build = false;
                }
                if ("Supplier".equals(currentName)) {
                  supplierMap.put(supplier.getName(), supplier);
                  supplier = null;
                }
              }
              if ("SupplierList".equals(currentName)) {
                state = State.MODE_LIST;
              }
          }
          currentName = null;
          break;
      }
    }

    response.getSuppliers().addAll(supplierMap.values());

    return response;
  }
}
